<template>
  <a-modal v-model:visible="modalVisible" @ok="handleOk" @cancel="handleCancel">
    <template #title> 选择分配成员 </template>
    <div>
      <a-form :model="form" auto-label-width>
        <a-form-item field="roleName" label="成员名称：">
          <a-select
            :style="{ width: '320px' }"
            placeholder="请选择需要分配的成员"
            v-model="Personnel"
            allow-search
          >
            <a-option
              v-for="item in PersonnelInfoData"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</a-option
            >
          </a-select>
        </a-form-item>
      </a-form>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { reactive, computed, ref, watchEffect } from 'vue';
  import { GetPersonnelinformation } from '@/api/home/System settings/personal-information/index';

  const props = withDefaults(
    defineProps<{
      visible: boolean;
    }>(),
    {
      visible: false,
    }
  );

  const emit = defineEmits(['update:visible', 'assignment']);

  // 判断用户是否显示弹出框
  const modalVisible = computed({
    get() {
      return props.visible;
    },
    set(visible: boolean) {
      emit('update:visible', visible);
    },
  });

  const PersonnelInfoData = ref();
  const getPersonnelInfo = async () => {
    const res = await GetPersonnelinformation({ page: 1, size: 9999 });
    PersonnelInfoData.value = res.content.data;
  };
  getPersonnelInfo();

  const Personnel = ref();
  const rules = reactive({});

  // 确定
  const handleOk = async () => {
    emit('assignment', Personnel.value);
    modalVisible.value = false;
  };

  // 取消
  const handleCancel = () => {
    modalVisible.value = false;
  };

  watchEffect(() => {
    if (props.visible) {
      Personnel.value = null;
    }
  });
</script>

<style scoped lang="less"></style>
